package com.example.voltorb.flip;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class TileAdapter extends BaseAdapter {
	
	Context context;
	List<Tile> items;
	
	public TileAdapter(Context c, List<Tile> tiles) {
		super();
		this.context = c;
		this.items = tiles;
	}
	
	@Override
	public boolean areAllItemsEnabled() {
		return false;
	}

	@Override
	public boolean isEnabled(int position) {
		return !this.getItem(position).isTurned();
	}

	public int getCount() {
		return this.items.size();
	}

	public Tile getItem(int position) {
		return this.items.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View v = new View(this.context);
//		LayoutInflater li = (LayoutInflater) this.context
//			.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//		v = li.inflate(R.layout.tile_layout, null);
//		LinearLayout base = (LinearLayout) v.findViewById(R.id.base_layout);
		
		if (this.isEnabled(position)) {
			// Show backside of the tile
			v = new LinearLayout(this.context);
			v.setMinimumHeight(100);
			
//			final int ROWS = 3;
//			final int COLS = 3;
//			LinearLayout[][] layouts = new LinearLayout[ROWS][COLS];
//			for (LinearLayout[] ll : layouts) {
//				for (LinearLayout l : ll) {
//					l = new LinearLayout(this.context);
//					l.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1));
//					l.setBackgroundColor(Color.rgb(0, 102, 0));
//				}
//			}

			LinearLayout ll = new LinearLayout(this.context);
			ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1));
			ll.setOrientation(LinearLayout.VERTICAL);
			LinearLayout ll2 = new LinearLayout(this.context);
			ll2.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1));
			ll2.setOrientation(LinearLayout.VERTICAL);
			
			TextView ll3 = new TextView(this.context);
			ll3.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1));
			ll3.setBackgroundColor(Color.rgb(102, 204, 102));
			if (this.getItem(position).isMarked()) {
				ll3.setGravity(Gravity.CENTER);
				ll3.setText("*");
				ll3.setTextColor(Color.rgb(255, 204, 0));
				ll3.setTextSize(20);
				ll3.setTypeface(null, Typeface.BOLD);
			}
			
			LinearLayout ll4 = new LinearLayout(this.context);
			ll4.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1));
			ll4.setBackgroundColor(Color.rgb(0, 102, 0));
			ll.addView(ll3);
			ll.addView(ll4);
			
			LinearLayout ll5 = new LinearLayout(this.context);
			ll5.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1));
			ll5.setBackgroundColor(Color.rgb(0, 102, 0));
			LinearLayout ll6 = new LinearLayout(this.context);
			ll6.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1));
			ll6.setBackgroundColor(Color.rgb(102, 204, 102));
			ll2.addView(ll5);
			ll2.addView(ll6);
			
			((LinearLayout) v).addView(ll);
			((LinearLayout) v).addView(ll2);
			
			return v;
		}
		
		Tile curTile = this.getItem(position);
		if (curTile instanceof CoinTile) {
			v = new TextView(this.context);
			
			v.setBackgroundColor(Color.rgb(204, 102, 102));
			
			((TextView) v).setHeight(100);
			((TextView) v).setGravity(Gravity.CENTER);
			((TextView) v).setText("" + ((CoinTile) curTile).getCoinAmount());
			((TextView) v).setTextColor(Color.rgb(0, 0, 0));
			((TextView) v).setTextSize(20);
			((TextView) v).setTypeface(null, Typeface.BOLD);
		} else if (curTile instanceof VoltorbTile) {
			v = new ImageView(this.context);
			
			((ImageView) v).setMaxHeight(100);
			((ImageView) v).setImageResource(R.drawable.ic_launcher);
			
			((ImageView) v).setBackgroundColor(Color.rgb(204, 102, 102));
			v.setPadding(2, 2, 2, 2);
		} else if (curTile instanceof SummaryTile) {
			v = new LinearLayout(this.context);
			v.setMinimumHeight(100);
			v.setBackgroundColor(Color.rgb(204, 204, 204));
			((LinearLayout) v).setOrientation(LinearLayout.VERTICAL);

			TextView tv = new TextView(this.context);
			tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1));
			tv.setGravity(Gravity.RIGHT);
			tv.setText(String.format("%02d", ((SummaryTile) curTile).getCoinAmount()));
			tv.setTextColor(Color.rgb(0, 0, 0));
			tv.setTextSize(16);
			tv.setTypeface(null, Typeface.BOLD);

			LinearLayout ll = new LinearLayout(this.context);
			ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1));
			
			ImageView iv = new ImageView(this.context);
			iv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1));
			Bitmap bm = BitmapFactory.decodeResource(this.context.getResources(), R.drawable.ic_launcher);
			int width = 50;
			int height = 50;
			Bitmap resizedbitmap = Bitmap.createScaledBitmap(bm, width, height, true);
			iv.setImageBitmap(resizedbitmap);
//			iv.setImageResource(R.drawable.ic_launcher);
			
			iv.setPadding(2, 2, 2, 2);
			
			TextView tv2 = new TextView(this.context);
			tv2.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1));
			tv2.setGravity(Gravity.CENTER);
			tv2.setText("" + ((SummaryTile) curTile).getVoltorbs());
			tv2.setTextColor(Color.rgb(0, 0, 0));
			tv2.setTextSize(16);
			tv2.setTypeface(null, Typeface.BOLD);
			
			ll.addView(iv);
			ll.addView(tv2);
			
			((LinearLayout) v).addView(tv);
			((LinearLayout) v).addView(ll);
		}
		
//		v.setPadding(4, 4, 4, 4);
		
		return v;
	}
}
